package com.haoan.index.service.impl;


import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.FoodMapper;
import com.haoan.index.domain.Food;
import com.haoan.index.service.FoodService;

/**
 * 美食Service业务层处理
 *
 * @author autoGen
 * @date 2025-02-21
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper,Food> implements FoodService
{
    /**
     * 查询美食
     *
     * @param id 美食主键
     * @return 美食
     */
    @Override
    public Food selectFoodById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询美食列表
     *
     * @param food 美食
     * @return 美食
     */
    @Override
    public List<Food> selectFoodList(Food food)
    {
        LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(food.getId() != null, Food::getId, food.getId());
        queryWrapper.like(food.getName() != null, Food::getName, food.getName());
        queryWrapper.eq(food.getFoodType() != null, Food::getFoodType, food.getFoodType());
        queryWrapper.eq(food.getPicture() != null, Food::getPicture, food.getPicture());
        queryWrapper.like(food.getEra() != null, Food::getEra, food.getEra());
        queryWrapper.like(food.getTexture() != null, Food::getTexture, food.getTexture());
        queryWrapper.like(food.getRegion() != null, Food::getRegion, food.getRegion());
        queryWrapper.like(food.getAddress() != null, Food::getAddress, food.getAddress());
        queryWrapper.eq(food.getPrice() != null, Food::getPrice, food.getPrice());
        queryWrapper.eq(food.getDetail() != null, Food::getDetail, food.getDetail());
        queryWrapper.eq(food.getLikes() != null, Food::getLikes, food.getLikes());
        queryWrapper.eq(food.getCreateTime() != null, Food::getCreateTime, food.getCreateTime());
        queryWrapper.eq(food.getCreateBy() != null, Food::getCreateBy, food.getCreateBy());
        queryWrapper.eq(food.getUpdateTime() != null, Food::getUpdateTime, food.getUpdateTime());
        queryWrapper.eq(food.getUpdateBy() != null, Food::getUpdateBy, food.getUpdateBy());
        queryWrapper.eq(Food::getValiFlag,ValiFlag.VALID );
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增美食
     *
     * @param food 美食
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFood(Food food)
    {
        return baseMapper.insert(food);
    }

    /**
     * 修改美食
     *
     * @param food 美食
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFood(Food food)
    {
        return baseMapper.updateById(food);
    }

    /**
     * 批量删除美食
     *
     * @param ids 需要删除的美食主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFoodByIds(Long [] ids)
    {
        Food food = new Food();
        food.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<Food> updQw = new LambdaQueryWrapper<>();
        updQw.in(Food::getId, ids);
        return baseMapper.update(food, updQw);
    }

    /**
     * 删除美食信息
     *
     * @param id 美食主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        Food food = getById(id);
        food.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(food);
    }
}
